package com.itheima.utils;

import com.itheima.pojo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.yaml.snakeyaml.constructor.DuplicateKeyException;

@RestControllerAdvice
@Slf4j
public class GloableExceptionHandler {
   @ExceptionHandler(value = Exception.class)
    public Result abc(Exception e){
       e.printStackTrace();
       log.error("GloableExceptionHandler类中abc方法"+e.getMessage());
       return Result.error("内部服务器错误.请联系管理员");
   }
   @ExceptionHandler(value = DuplicateKeyException.class)
    public Result abc(DuplicateKeyException e) {
       e.printStackTrace();
       if (e.getMessage().contains("dept.name")) {

           return Result.error("部门名称不能重复");
       }
       if (e.getMessage().contains("emp.username")) {
           return Result.error("员工名称不能重复");
       }
       return Result.error("服务器内部错误,请联系管理员");
   }

}
